<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { getStudentInfo, updateStudentInfo } from '../../../api/student'

const userInfo = ref({})
const isEditing = ref(false)
const editForm = ref({})

// 获取用户信息
const fetchUserInfo = async () => {
  try {
    const data = await getStudentInfo()
    userInfo.value = data
  } catch (error) {
    ElMessage.error(error || '获取学生信息失败')
  }
}

// 开始编辑
const startEdit = () => {
  editForm.value = { ...userInfo.value }
  isEditing.value = true
}

// 保存编辑
const saveEdit = async () => {
  try {
    await updateStudentInfo(editForm.value)
    await fetchUserInfo()
    isEditing.value = false
    ElMessage.success('更新成功')
  } catch (error) {
    ElMessage.error(error || '更新失败')
  }
}

// 取消编辑
const cancelEdit = () => {
  isEditing.value = false
}

onMounted(() => {
  fetchUserInfo()
})
</script>

<template>
  <div class="profile-container">
    <el-card class="profile-card">
      <template #header>
        <div class="card-header">
          <span class="title">个人信息</span>
          <div class="header-actions">
            <el-button v-if="!isEditing" type="primary" @click="startEdit">编辑</el-button>
            <template v-else>
              <el-button type="success" @click="saveEdit">保存</el-button>
              <el-button @click="cancelEdit">取消</el-button>
            </template>
          </div>
        </div>
      </template>

      <div class="info-list">
        <template v-if="!isEditing">
          <!-- 展示模式 -->
          <div class="info-item" v-for="(field, label) in {
            '学号': 'studentNumber',
            '姓名': 'name',
            '班级': 'className',
            '学校': 'school',
            '性别': 'gender',
            '手机号': 'phone'
          }" :key="field">
            <span class="info-label">{{ label }}：</span>
            <span class="info-content">
              <template v-if="field === 'gender'">
                {{ userInfo[field] === 1 ? '男' : '女' }}
              </template>
              <template v-else>
                {{ userInfo[field] }}
              </template>
            </span>
          </div>
        </template>

        <template v-else>
          <!-- 编辑模式 -->
          <el-form :model="editForm" label-width="120px">
            <el-form-item label="学号">
              <el-input v-model="editForm.studentNumber" disabled />
            </el-form-item>
            <el-form-item label="姓名">
              <el-input v-model="editForm.name" />
            </el-form-item>
            <el-form-item label="班级">
              <el-input v-model="editForm.className" />
            </el-form-item>
            <el-form-item label="学校">
              <el-input v-model="editForm.school" />
            </el-form-item>
            <el-form-item label="性别">
              <el-radio-group v-model="editForm.gender">
                <el-radio :label="1">男</el-radio>
                <el-radio :label="0">女</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="手机号">
              <el-input v-model="editForm.phone" />
            </el-form-item>
          </el-form>
        </template>
      </div>
    </el-card>
  </div>
</template>

<style scoped>
.profile-container {
  padding: 40px;
  background-color: #f5f7fa;
  min-height: calc(100vh - 60px);
}

.profile-card {
  max-width: 1000px;
  margin: 0 auto;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
  border-radius: 8px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 0;
}

.title {
  font-size: 24px;
  font-weight: bold;
  color: #303133;
}

.info-list {
  padding: 20px;
}

.info-item {
  display: flex;
  padding: 16px 24px;
  border-bottom: 1px solid #ebeef5;
  transition: background-color 0.3s;
}

.info-item:last-child {
  border-bottom: none;
}

.info-item:hover {
  background-color: #f5f7fa;
}

.info-label {
  width: 120px;
  color: #606266;
  font-weight: bold;
  font-size: 16px;
}

.info-content {
  flex: 1;
  color: #303133;
  font-size: 16px;
}

/* 响应式调整 */
@media (max-width: 768px) {
  .info-item {
    padding: 12px 16px;
  }

  .info-label {
    width: 100px;
    font-size: 14px;
  }

  .info-content {
    font-size: 14px;
  }
}

.header-actions {
  display: flex;
  gap: 10px;
}

.el-form {
  max-width: 600px;
  margin: 0 auto;
}

.el-form-item {
  margin-bottom: 20px;
}
</style>
